/*
 * UserDao
 * 
 * Version 1.0
 *
 * 23-08-08
 * 
 * Modification Logs:
 *   DATE        AUTHOR      DESCRIPTION
 *  --------------------------------------------------------
 *  23-08-08	TuHA		declare class prototype
 *  29-08-08	TuHA		code functions:checkPasswd
 */

package vn.fpt.fsoft.fcos.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import vn.fpt.fsoft.fcos.entity.UserEntity;

/**
 * UserDao: all of methods use to get user data from the database
 * @author TuHa
 *
 */
public class UserDao {

	/**
	 * 
	 * @param user
	 * @return
	 */
	public String create(UserEntity user) {
		return "";
	}
	
	/**
	 * 
	 * @param user
	 * @return
	 */
	public String update(UserEntity user) {
		return "";
	}
	
	/**
	 * 
	 * @param userId
	 * @param passwd
	 * @return
	 */
	public boolean changePasswd(String userId, String passwd) {
		return true;
	}
	
	/**
	 * checkPasswd check if user (userId, passwd) is existed in database
	 * @param userId is userId of user
	 * @param passwd is password of user
	 * @return the boolean express whether the user is existed in database
	 * 			true if user is in database
	 * 			false if user is not in database
	 */
	public boolean checkPasswd(String userId, String passwd) {
		DaoService service = new DaoService();
		
		String sql = "select count(User_ID) "
					+ "from User_Master "
					+ "where User_ID = ${userId} "
					+ "and Password = ${passwd} ";
		
		Map values = new HashMap();
		values.put("userId", userId);
		values.put("passwd", passwd);
		
		PreparedStatement pstm = DaoService.buildSQL(service.getConnection(), sql, values);
		ResultSet results = null;		
		
		int count = 0;
		
		try {
			results = pstm.executeQuery();
			results.next();
			
			count = results.getInt(1);
		} catch (Exception e) {
			return false;
		}
		
		return count > 0;
	}
	
	public UserEntity getUserEntity(String userId, String passwd) {
		DaoService service = new DaoService();
		UserEntity user = null;
		
		String sql = "select *"
					+ "from User_Master "
					+ "where User_ID = ${userId} "
					+ "and Password = ${passwd} ";
		
		Map values = new HashMap();
		values.put("userId", userId);
		values.put("passwd", passwd);
		
		PreparedStatement pstm = DaoService.buildSQL(service.getConnection(), sql, values);
		ResultSet results = null;		
						
		try {
			results = pstm.executeQuery();
			
			if (!results.next()) {
				user.setUserId(results.getString("User_ID"));
				user.setPassword(results.getString("Password"));
				user.setUserName(results.getString("User_Name"));
				user.setUserEmail(results.getString("User_Email"));
				user.setUserMobile(results.getString("User_Mobile"));
				user.setLocationCd(results.getString("Location_CD"));
				user.setUserLevel(results.getInt("User_Level"));
				user.setNumberOfLogin(results.getInt("Number_of_Login"));
				user.setLastLoginDate(results.getDate("Last_Login_Date"));				
			}
		} catch (Exception e) {
			return null;
		}
				
		return user;	
	}
	
	/**
	 * getLevel function is used to get level of an user
	 * @param userId is user id
	 * @param passwd is user password
	 * @return level of this user 
	 */
	public int getLevel(String userId, String passwd) {
		DaoService service = new DaoService();
		
		String sql = "select User_Level "
					+ "from User_Master "
					+ "where User_ID = ${userId} "
					+ "and Password = ${passwd} ";
		
		Map values = new HashMap();
		values.put("userId", userId);
		values.put("passwd", passwd);
		
		PreparedStatement pstm = DaoService.buildSQL(service.getConnection(), sql, values);
		ResultSet results = null;		
		
		int lev = -1;
		
		try {
			results = pstm.executeQuery();
			results.next();
			
			lev = results.getInt(1);
		} catch (Exception e) {
			return -1;
		}
				
		return lev;
	}
	
	public static void main(String[] args) {
		UserDao userDao = new UserDao();
			
		int lv = userDao.getLevel("tuha", "12");
		
	}
}
